Failed Conditions
Push — master ( c3e11c...1d8d9e )
by Yo
02:09 queued 37s
created

Router.get(ꞌ/portal*ꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
var express = require('express');
2
var extend  = require('extend');
3
var request = require('request');
4
5
// ------------------------------------------
6
//  CONSTRUCTOR
7
// ------------------------------------------
8
9
var init = function(){
10
  info('Starting PortalManager ...');
11
  
12
  // Load sidebar
13
  loadSidebar();
14
  
15
  // Refresh ticker
16
  refreshTicker();
17
  
18
  // Refresh ticker
19
  refreshRSSNews();
20
  
21
  return PortalManager;
22
}
23
24
// ------------------------------------------
25
//  SIDEBAR
26
// ------------------------------------------
27
28
var portal  = {}; 
29
var loadSidebar = function(){
30
  
31
  PortalManager.SIDEBAR = {
32
    'nav' : [
33
      { name : 'nav.store'       ,  url : '/portal/store' },
34
      { name : 'nav.rules'       ,  url : '/portal/rules' },
35
      { name : 'nav.experimental',  url : '/portal/experimental' },
36
      { name : 'nav.config'       , url : '/portal/config' },
37
      { name : 'nav.doc'         ,  url : '/portal/doc' },
38
      { name : 'nav.about'       ,  url : '/portal/about' }
39
    ],
40
    
41
    'links' : [
42
      { name : 'nav.encausse'    , url : 'http://sarah.encausse.net' },
43
      { name : 'nav.community'   , url : 'http://community.sarah.encausse.net' },
44
      { name : 'nav.marketplace' , url : 'http://marketplace.sarah.encausse.net' },
45
      { name : 'nav.map'         , url : 'http://map.sarah.encausse.net' }
46
    ]
47
  }
48
}
49
50
// ------------------------------------------
51
//  RENDER
52
// ------------------------------------------
53
54
var fs  = require('fs');
55
var ejs = require('ejs');
56
var render = function(path, options){
57
  var path = require('path').normalize(__dirname+'/../../' + path);
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
58
  if (!fs.existsSync(path)){ return "<h4>File not found: "+path+"</h4>"; }
59
  
60
  var text = fs.readFileSync(path, 'utf8');
61
  return ejs.render(text, options);
62
};
63
64
// ------------------------------------------
65
//  ROUTER
66
// ------------------------------------------
67
68
var Router = express.Router();
69
70
// COMMON
71
72
Router.all('/', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
73
  res.redirect('/portal');
74
});
75
76
Router.all('*', function(req, res, next) {
77
  res.locals.req = req;
78
  res.locals.res = res;
79
  res.locals.require = require;    
80
  res.locals.message = false; 
81
  next();
82
});
83
84
Router.get('/confirm', function(req, res, next) { 
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
85
  return res.render('layout/confirm.ejs', {});
86
});
87
88
// PORTAL
89
90
Router.get('/portal*', function(req, res, next) {
91
  var menu = {}; extend(true, menu, PortalManager.SIDEBAR);
92
  res.locals.render  = render;
93
  res.locals.sidebar = menu;
94
  next();
95
});
96
97
Router.get('/portal', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
98
  res.render('portal/portal.ejs');
99
});
100
101
Router.get('/portal/rules', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
102
  res.locals.sidebar.nav[1].active = true;
103
  res.render('rules/rules.ejs');
104
});
105
106
Router.get('/portal/experimental', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
107
  res.locals.sidebar.nav[2].active = true;
108
  res.render('experimental/experimental.ejs');
109
});
110
111
Router.get('/portal/doc', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
112
  res.locals.sidebar.nav[3].active = true;
113
  res.render('portal/doc.ejs');
114
});
115
116
Router.get('/portal/about', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
117
  res.locals.sidebar.nav[4].active = true;
118
  res.render('portal/about.ejs');
119
});
120
121
122
// ------------------------------------------
123
//  TICKER
124
// ------------------------------------------
125
126
var ticker = '&nbsp;';
127
var refreshTicker = function(){
128
  var url = 'http://ticker.sarah.encausse.net';
129
  request({ 
130
    'uri' : url, 
131
    'json' : true, 
132
    'headers': {'user-agent': SARAH.USERAGENT } 
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
133
  }, 
134
  function (err, response, json){
135
    if (err || response.statusCode != 200) { 
136
      return warn("Can't retrieve remote ticker");   
137
    }
138
    ticker = json.message;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
139
  });
140
  return ticker;
141
}
142
143
var refreshRSSNews = function(){
144
  var RSS = 'http://news.sarah.encausse.net';
145
  return SARAH.getRSSFeed(RSS);
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
146
}
147
148
// ------------------------------------------
149
//  PUBLIC
150
// ------------------------------------------
151
152
var PortalManager = {
153
  'init' : init,
154
  'refreshTicker' : refreshTicker,
155
  'refreshRSSNews': refreshRSSNews,
156
  'Router' : Router,
157
}
158
159
// Exports Manager
160
exports.init = PortalManager.init;